class UsersController < ApplicationController

  before_filter :require_user, :require_admin, :only => 'index'

  def new
    @user = User.new
  end

  def create
    #raise params[:user].to_s
    @user = User.new(params[:user])
    if @user.save
      flash[:notice] = "User Created!"
      redirect_to(dashboard_path)
    else
      render :new
    end
  end

  def edit
    @user = User.find(params[:id])
    if !current_user.is_admin? && @user != current_user
      redirect_to(edit_user_path(current_user.user_id))
    end
  end

  def update
    @user = User.find(params[:id])
    if @user.update_attributes(params[:user])
      flash[:notice] = "User #{@user.email} successfully updated."
      redirect_to(users_path)
    else
      render :action => "edit"
    end
  end

  def index
    @users = User.all
  end

  def destroy
    @user = User.find(params[:id])
    @user.destroy
    flash[:notice] = "User #{@user.email} successfully deleted."
    redirect_to(index_users_path)
  end
end
